package com.zry.yard.ui.activity


import android.content.Intent
import android.util.Log
import com.zry.yard.R
import com.zry.yard.been.BatchNextInfo
import com.zry.yard.databinding.ActivityTraceabilityCreateBinding
import com.zry.yard.presenter.TraceabilityCreatePresenter
import com.zry.yard.ui.base.BaseActivity
import com.zry.yard.utils.ACacheUtil
import com.zry.yard.utils.DateTimeUtilsKt
import kotlinx.android.synthetic.main.activity_traceability_create.*
import kotlinx.android.synthetic.main.common_title.*
import me.nereo.multi_image_selector.MultiImageSelector


class TraceabilityCreateActivity : BaseActivity() {

    private var traceabilityCreatePresenter: TraceabilityCreatePresenter? = null
    private var traceabilityCreateBinding: ActivityTraceabilityCreateBinding? = null

    var batchNextInfo: BatchNextInfo? = null
    var imageList: ArrayList<String>? = null
    val REQUEST_IMAGE = 2
    var batchID: String? = null

//    var provinceStrCode: String? = null
//    var cityStrCode: String? = null
//    var districtStrCode: String? = null

//    private val conn = object : ServiceConnection {
//        override fun onServiceConnected(name: ComponentName, service: IBinder) {}
//
//        override fun onServiceDisconnected(name: ComponentName) {}
//    }

    override fun getLayoutId(): Int {
        return R.layout.activity_traceability_create
    }

    override fun initData() {
        titleCenterText.text = "溯源录入"
        commonLeftLinearLayout.setOnClickListener { finish() }
//        EventBus.getDefault().register(this)

        batchNextInfo = intent.getSerializableExtra("batchNextInfo") as BatchNextInfo
        batchID = intent.getStringExtra("batchID")
        traceabilityCreateBinding = viewDataBinding as ActivityTraceabilityCreateBinding
        traceabilityCreatePresenter = TraceabilityCreatePresenter(this)
        traceabilityCreateBinding?.handlerClick = traceabilityCreatePresenter
        traceabilityCreatePresenter?.initOPeraSpinner(selectNodeSpinner)
        traceabilityOperatorTV.setText(ACacheUtil().getLoginInfo().UserName)
        imageList = arrayListOf()
        imageList?.add("默认图片")
        selectTimeTV.text = DateTimeUtilsKt().getNowDateTime("yyyy-MM-dd")
        traceabilityCreatePresenter?.initRecycler()

//
//        val intent = Intent(this, BaiDuService::class.java)
//        this.bindService(intent, conn, Service.BIND_AUTO_CREATE)

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK) {
            return
        }
        if (requestCode == REQUEST_IMAGE) {
            val imagePathList = data!!.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT)

            val imageFilterList = imagePathList.filterNot { it == "默认图片" }
            Log.e("TAG", imageFilterList.toString())
            imageList!!.removeAll(imageList!!)
            imageList!!.addAll(imageFilterList)
            imageList!!.add("默认图片")
            traceabilityCreatePresenter?.addImageAdapter?.notifyDataSetChanged()
        }
    }

//
//    @SuppressLint("SetTextI18n")
//    @Subscribe(threadMode = ThreadMode.POSTING)
//    fun onMoonEvent(location: BDLocation) {
//        val latitude = location.latitude    //获取纬度信息
//        val longitude = location.longitude    //获取经度信息
//
//        val adCode = location.adCode
//        val address = location.address
//        Log.e("Code", adCode)
//        Log.e("Code", address.address)
//
//        provinceStrCode = StringListUtil().getStartString(adCode, 2) + "0000"
//        cityStrCode = StringListUtil().getStartString(adCode, 2) + StringListUtil().getCenterString(adCode, 2, 4) + "00"
//        districtStrCode = adCode
//        Log.e("Code", provinceStrCode)
//        Log.e("Code", cityStrCode)
//        Log.e("Code", adCode)
//
//        operatingPlaceTV.text = location.province + location.city + location.district
//        detailedAddressTV.setText(location.street + location.streetNumber)
//
//        if ( !operatingPlaceTV.text .isEmpty()){
//            EventBus.getDefault().post(StopClient("获取到了地址"))
//        }
//    }

    override fun onDestroy() {
//        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

}
